// pages/index/class/chooseCourse/chooseCourse.js
const util = require("../../../../utils/util.js");
const config = require("../../../../utils/config.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    items: [],
    chooseItem:[],//选课结果
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    util.setBarColor()
    this.getData()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  getData:function(){
    var that = this;
    let url = config.BaseUrl + "t/course/class/teacher/list";
    let data = {};
    util.request(url, data, "GET").then(function (res) {
      let items = res.data.list
      for(var i in items){
        if (items[i].courseNum == items[i].courseHasNum){
          items[i]["disabled"] = true
        }
      }
      if (res.code == 200) {
        that.setData({
          items: items
        })
      }
    })
  },
  checkboxChange(e) {
    console.log('checkbox发生change事件，携带value值为：', e.detail.value)
    var that = this
    that.setData({
      chooseItem: e.detail.value
    })
  },

  submit:function(){
    var that = this
    var chooseItem = that.data.chooseItem
    var userId = wx.getStorageSync("userInfo").id;
    var result = []
    for (var i in chooseItem){
      let data = {}
      data["courseId"] = chooseItem[i],
      data["userId"] = userId
      result.push(data);
    }
    let url = config.BaseUrl + "t/course/user/addList";
    let data = result;
    util.request(url, data, "GET").then(function (res) {
      if (res.code == 200) {
        wx.showToast({
          title: '添加成功'
        })
      }
    })
  }
})